package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.MemberDTO;
import mju.cxf.entity.Activity;
import mju.cxf.entity.Admin;
import mju.cxf.entity.Member;
import mju.cxf.exception.BaseException;
import mju.cxf.mapper.MemberMapper;
import mju.cxf.service.MemberService;
import mju.cxf.utils.TimestampUpdaterUtils;
import mju.cxf.vo.ActivityVO;
import mju.cxf.vo.MemberVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 郑献
 * @since 2024-10-23
 */

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    /**
     * A分页查询会员信息
     * @param grade
     * @return
     */
    @Override
    public Page<MemberVO> selectPage(Integer grade, int current, int size) {
        Page<MemberVO> page = new Page<>(current, size);
        return this.baseMapper.selectPageVo(page,grade);
    }

    /**
     * A新增会员信息
     * @param memberDTO
     * @return
     */
    @Override
    public boolean saveMember(MemberDTO memberDTO) {
        // 检查是否存在相同等级的会员
        int count = (int) this.count(new QueryWrapper<Member>().eq("grade", memberDTO.getGrade()));
        if (count > 0) {
            // 如果存在相同等级的会员，返回false或抛出异常
            throw new BaseException(MessageConstant.DATA_DUPLICATION);
        }

        Member member = new Member();
        memberDTO.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(memberDTO, member);
        TimestampUpdaterUtils.saveUtil(member);
        return this.save(member); // 使用 MyBatis-Plus 提供的 save() 方法
    }


    /**
     * A修改会员信息
     * @param memberDTO
     * @return
     */
    @Override
    public boolean updateMember(MemberDTO memberDTO) {
        Member member = new Member();
        memberDTO.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(memberDTO,member);
        return this.updateById(member);
    }

    /**
     * A删除会员信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteMember(Integer id) {
        return this.removeById(id);
    }
}
